package com.example.a2022application.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.a2022application.BaseActivity;
import com.example.a2022application.R;
import com.example.a2022application.bean.RegisterBean;
import com.example.a2022application.net.Urls;
import com.example.a2022application.utils.SpUtils;
import com.example.a2022application.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import androidx.annotation.Nullable;

public class AddMessageActivity extends BaseActivity implements View.OnClickListener {
    private TextView tv_add;
    private EditText et_msg;
    private String gname;
    private String objid;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addmessage);
        tv_add = findViewById(R.id.tv_add);
        et_msg = findViewById(R.id.et_msg);
        tv_add.setOnClickListener(this);
        Intent intent = getIntent();
        gname = intent.getStringExtra("gname");
        objid = intent.getStringExtra("objid");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_add:
                String msg = et_msg.getText().toString();
                if (TextUtils.isEmpty(msg)){
                    ToastUtil.showToast("请输入留言");
                    return;
                }
                String userid = SpUtils.getString(AddMessageActivity.this,"userid");
                String username = SpUtils.getString(AddMessageActivity.this,"nickname");
                if (TextUtils.isEmpty(userid)){
                    ToastUtil.showToast("请先登录");
                    return;
                }
                if ("teachermsg".equals(gname)){
                    OkGo.<String>post(Urls.UPDATETASKMAG)
                            .params("obj_id",objid)
                            .params("teachermsg",msg)
                            .execute(new StringCallback() {
                                @Override
                                public void onSuccess(Response<String> response) {
                                    RegisterBean bean = getGson().fromJson(response.body(),RegisterBean.class);
                                    if (bean.getCode() == 0){
                                        finish();
                                    }
                                    ToastUtil.showToast(bean.getMessage());
                                }
                            });

                }else {
                    OkGo.<String>post(Urls.GROUPMESSAGE)
                            .params("groupName",gname)
                            .params("gmessage",msg)
                            .params("gmessageuserid",userid)
                            .params("gmessageusername",username)
                            .execute(new StringCallback() {
                                @Override
                                public void onSuccess(Response<String> response) {
                                    RegisterBean bean = getGson().fromJson(response.body(),RegisterBean.class);
                                    if (bean.getCode() == 0){
                                        finish();
                                    }
                                    ToastUtil.showToast(bean.getMessage());
                                }
                            });
                }


                break;

        }
    }
}
